const db = require("../config/db");

exports.getAll = async () => {
  const [rows] = await db.query("SELECT * FROM activities");
  return rows;
};

exports.add = async (activity) => {
  const { name, type, status, startTime, endTime, image } = activity;
  const [result] = await db.query(
    "INSERT INTO activities (name,type,status,startTime,endTime,image) VALUES (?,?,?,?,?,?)",
    [name, type, status, startTime, endTime, image]
  );
  return { id: result.insertId, ...activity };
};

exports.update = async (id, activity) => {
  const { name, type, status, startTime, endTime, image } = activity;
  await db.query(
    "UPDATE activities SET name=?,type=?,status=?,startTime=?,endTime=?,image=? WHERE id=?",
    [name, type, status, startTime, endTime, image, id]
  );
  return { id, ...activity };
};

exports.remove = async (id) => {
  await db.query("DELETE FROM activities WHERE id=?", [id]);
};
